﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestPathFinding : MonoBehaviour
{

    //用一个数组接收所有的路径点对象
    public List<Vector3> wayPoint;
    public Transform target;
    public Vector3 oldPos;
    public float speed;
    //记录下一个路径点的索引值
    public int nextPointIndex;
    public float t;
    // Use this for initialization
    void Start()
    {
        wayPoint = new List<Vector3>();
    }

    // Update is called once per frame
    void Update()
    {
        if (target == null)
        {
            PlayerCtrl pc = FindObjectOfType<PlayerCtrl>();
            if (pc != null) target = pc.transform;
            return;
        }
        if (oldPos != target.position)
        {
            FindPathUtl.FindPathAir(transform.position, target.position, wayPoint);
            oldPos = target.position;
            nextPointIndex = 1;
            t = 0;
        }
        if (wayPoint.Count == 0) return;
        transform.position = Vector3.Lerp(transform.position, wayPoint[nextPointIndex], t);
        t += Time.deltaTime * speed;
        if (Vector3.Distance(transform.position, wayPoint[nextPointIndex]) < 0.1f)
        {
            t = 0;
            nextPointIndex++;
            if (nextPointIndex >= wayPoint.Count-1)
            {
                nextPointIndex = 0;
                wayPoint.Clear();
            }
        }
    }
}
